<?php

/**
 * This is the model class for table "reg_student".
 *
 * The followings are the available columns in table 'reg_student':
 * @property string $student_id
 * @property integer $prefix_id
 * @property string $student_name
 * @property string $student_surname
 * @property string $department
 * @property string $major
 * @property string $plan
 * @property string $period
 * @property string $adviser
 * @property string $student_status
 * @property string $remark
 * @property string $receipt_no
 * @property string $user_id
 * @property string $created_by
 * @property string $created_date
 * @property string $modified_by
 * @property string $modified_date
 */
class RegStudent extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'reg_student';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('student_id, prefix_id, student_name, student_surname, department, major, plan, period, adviser, student_status, remark, receipt_no, user_id, created_by, created_date', 'required'),
			array('prefix_id', 'numerical', 'integerOnly'=>true),
			array('student_id, student_name, student_surname', 'length', 'max'=>150),
			array('department, adviser, remark', 'length', 'max'=>250),
			array('major, plan, period, student_status, receipt_no', 'length', 'max'=>50),
			array('user_id, created_by, modified_by', 'length', 'max'=>20),
			array('modified_date', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('student_id, prefix_id, student_name, student_surname, department, major, plan, period, adviser, student_status, remark, receipt_no, user_id, created_by, created_date, modified_by, modified_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'prefix' => array(self::BELONGS_TO, 'RegPrefix', 'prefix_id'),
				'student' => array(self::BELONGS_TO, 'Student', 'student_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'student_id' => 'Student',
			'prefix_id' => 'Prefix',
			'student_name' => 'Student Name',
			'student_surname' => 'Student Surname',
			'department' => 'Department',
			'major' => 'Major',
			'plan' => 'Plan',
			'section' => 'Section',
			'period' => 'Period',
			'adviser' => 'Adviser',
			'student_status' => 'Student Status',
			'remark' => 'Remark',
			'receipt_no' => 'Receipt No',
			'user_id' => 'User',
			'created_by' => 'Created By',
			'created_date' => 'Created Date',
			'modified_by' => 'Modified By',
			'modified_date' => 'Modified Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($type=null)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('student_id',$this->student_id,true);
		$criteria->compare('prefix_id',$this->prefix_id);
		$criteria->compare('student_name',$this->student_name,true);
		$criteria->compare('student_surname',$this->student_surname,true);
		$criteria->compare('department',$this->department,true);
		$criteria->compare('major',$this->major,true);
		$criteria->compare('plan',$this->plan,true);
		$criteria->compare('period',$this->period,true);
		$criteria->compare('section',$this->section,true);
		$criteria->compare('adviser',$this->adviser,true);
		$criteria->compare('student_status',$this->student_status,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('receipt_no',$this->receipt_no,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('modified_by',$this->modified_by,true);
		$criteria->compare('modified_date',$this->modified_date,true);
		//$criteria->with="prefix";
		//$criteria->with="student";
		$criteria->with=array("prefix","student.activeProject");
		//$criteria->join = "LEFT JOIN mms_student ON t.student_id=mms_student.studentID LEFT JOIN reg_prefix on t.prefix_id=reg_prefix.prefix_id";
		$criteria->compare('activeProject.projectName',$this->student->projects[0]->projectName,true);
		$criteria->compare('activeProject.statusCode',$this->student->projects[0]->statusCode,true);
		//$criteria->with="prefix";
		//if ($type){
		//echo $this->student->projects[0]->projectName;
		//exit;
		//}
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return RegStudent the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
